/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package SuperMarket;

import java.sql.*;
import SuperMarketGuis.LoginGui;

/**
 *
 * @author Ota2
 */
public class Loging_in {
	
    private static String dbURL = 
	"jdbc:derby://localhost:1527/SuperMarket;create=true;user=plh24;password=plh24";
    // jdbc Connection
    private static Connection conn = null;
    private static Statement stmt = null;
    private static String first_name = null;
    private static String last_name = null;
    private static int adminpr = 0;

    public static String getFirst_name() {
        return first_name;
    }

    public static String getLast_name() {
        return last_name;
    }

    public static int getAdminpr() {
        return adminpr;
    }

	public void Logingin()
    {
	adminpr=0;
	int i=1;
	while (i<4)
	{
	LoginGui login = new LoginGui(null, true);
        login.setLocationRelativeTo(null); 
	login.setVisible(true);
	String Username = login.getUsr();
	String Password = login.getPss();
	
	if ((Username.equals("admin"))&&(Password.equals("000-000")))
		{ 
		System.out.println("Welcome admin");
		adminpr = 1;
		first_name ="admin";
		last_name = "admin";
		break;
		}
	else
		{
                try
                    {
                    Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
                    //Get a connection
                    conn = DriverManager.getConnection(dbURL); 
                    stmt = conn.createStatement();
                    String query = ("SELECT first_name, last_name, adminpr FROM storeuser where points_card_number like '"
                    +Username+"' and password like '"+Password+"'");
                    ResultSet rst = null;
                    rst = stmt.executeQuery(query);
                    if (rst.next()) 
                        {
                        first_name = rst.getString(1);
                        last_name = rst.getString(2);
                        adminpr = rst.getInt(3);
                        break;
                        }
                    else
                        {
                        System.out.println ("Invalid Credentials");
                        System.out.println ("You have "+(3-i)+" tries left");
                        i++;
                        }
                    // Disconnect from database.
                    stmt.close();
                    conn.close();
                    }
                catch (Exception ex)
                    {
                    System.out.println(ex.getMessage());
                    }
                }
	}
    }
}